package com.metacube.questionbank.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.metacube.questionbank.dao.QuestionDAO;
import com.metacube.questionbank.dto.AnswerDTO;
import com.metacube.questionbank.service.Utility;
import com.metacube.questionbank.service.Validator;

@Controller
@RequestMapping("/answer")
public class AnswerController {
	
	@Autowired
	Validator validate;
	
	@Autowired
	QuestionDAO qDAO;
	
	@Autowired
	Utility utility;
	
	@RequestMapping(value = "postAnswers")
	@ResponseBody
	public List<AnswerDTO> postAnswer(@ModelAttribute AnswerDTO aDTO, HttpSession session, @RequestParam("qId") int qId,
			@RequestParam("aBody") String aBody) {
		
		aDTO.setaBody(aBody);
		System.out.println("a body"+aDTO.getaBody());
		
		int userId = (int) session.getAttribute("sessionId");
		
		utility.initAnswer(aDTO, qId, userId);
		
		return utility.getAnswers(qId);
			
	}
	
	
	@RequestMapping(value="myAnswers")
	@ResponseBody
	public List<AnswerDTO> getMyAnswers(HttpSession session){
		
		int id=(int)session.getAttribute("sessionId");
		
		return utility.getMyAnswers(id);
	}
	
	
	@RequestMapping(value="getAnswers")
	@ResponseBody
	public List<AnswerDTO> getAnswers(@RequestParam("qId") int qId,HttpServletRequest request){
		
		System.out.println(qId+"hello");
		
		return utility.getAnswers(qId);
	}


}
